<template>
	<div class="app-container">
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:examinationinfo:add']">添加考试</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
				 v-hasPermi="['system:examinationinfo:remove']">删除考试</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="examinationinfoList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="考试号" align="center" prop="safetyExaminationId" />
			<el-table-column label="考试名称" align="center" prop="safetyExaminationName" />
			<el-table-column label="开始时间" align="center" prop="startTime" />
			<el-table-column label="结束时间" align="center" prop="finishTime" />
			<el-table-column label="考试状态" align="center" prop="safetyExaminationType" />
			<!-- <el-table-column label="设置人员" align="center" prop="setId" /> -->
			<el-table-column label="设置时间" align="center" prop="setDate" />
			<el-table-column label="查看详情" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:examinationinfo:query']">查看详情</el-button>
				</template>
			</el-table-column>
			<el-table-column label="开始结束考试" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="Startexam(scope.row)" v-hasPermi="['system:examinationinfo:edit']">开始考试</el-button>
					<el-button size="mini" type="text" icon="el-icon-edit" @click="Finishexam(scope.row)" v-hasPermi="['system:examinationinfo:edit']">结束考试</el-button>
				</template>
			</el-table-column>
			<el-table-column label="相关内容" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<router-link :to="{path:'/winningsecurityexam',query:{ examinationId: scope.row.safetyExaminationId }}">
						<el-button type="text" icon="el-icon-edit" size="mini" v-hasPermi="['system:winningsecurityexam']">安全考试中奖</el-button>
					</router-link>
					<router-link :to="{path:'/setpaper',query:{ examinationId: scope.row.safetyExaminationId }}">
						<el-button type="text" icon="el-icon-edit-outline" size="mini" v-hasPermi="['system:examinationpaperseting']">设置试卷</el-button>
					</router-link>
					<router-link :to="{path:'/branchexamination',query:{ examinationId: scope.row.safetyExaminationId }}">
						<el-button type="text" icon="el-icon-edit-outline" size="mini" v-hasPermi="['system:branchexaminationinfo']">分院情况查看</el-button>
					</router-link>
					<router-link :to="{path:'/prizesetting',query:{ safetyExaminationId: scope.row.safetyExaminationId }}">
						<el-button type="text" icon="el-icon-check" size="mini" v-hasPermi="['system:prizeseting']">奖品设置</el-button>
					</router-link>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加考试对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="考试名称" prop="safetyExaminationName">
					<el-input v-model="form.safetyExaminationName" placeholder="请输入考试名称" />
				</el-form-item>
				<el-form-item label="开始时间" prop="startTime">
					<el-date-picker clearable size="md" style="width: 300px" v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
					 placeholder="选择开始时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="结束时间" prop="finishTime">
					<el-date-picker clearable size="md" style="width: 300px" v-model="form.finishTime" type="date" value-format="yyyy-MM-dd"
					 placeholder="选择结束时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="考试状态" prop="safetyExaminationType">
					<el-select v-model="form.safetyExaminationType" placeholder="请选择考试状态">
						<el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
					</el-select>
				</el-form-item>
				<!--<el-form-item label="试卷选择" prop="safetyExaminationPaperName">
		  <el-select v-model="form.safetyExaminationPaperName" placeholder="请选择考试状态">
		    <el-option v-for="dict in testpaperOptions" :key="dict.safetyExaminationPaperName" :value="dict.safetyExaminationPaperName" />
		  </el-select>
		</el-form-item> -->
				<el-form-item label="设置人员" prop="setId">
					<el-input v-model="form.setId" :readonly="true" placeholder="请输入设置人员" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listSafetyexam,
		listTestpaper,
		addSafetyexam,
		testPaperDetail,
		modifySafetyexam,
		deleteSafetyexam
	} from "@/api/system/safetyexam.js";
	import store from '@/store'
	export default {
		name: "Safetyexam",
		components: {},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				//考试状态字典
				typeOptions: [],
				//考试试卷
				testpaperOptions: [],
				// 考试信息表格数据
				examinationinfoList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					safetyExaminationId: null,
					safetyExaminationName: null,
					startTime: null,
					finishTime: null,
					safetyExaminationType: null,
					safetyExaminationPaperName: null,
					setId: null,
					setDate: null
				},
				//删除考试和开始考试的参数
				examParams: {
					safetyExaminationId: null,
					safetyExaminationType: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					safetyExaminationName: [{
						required: true,
						message: "考试名称不能为空",
						trigger: "blur"
					}],
					startTime: [{
						required: true,
						message: "开始时间不能为空",
						trigger: "blur"
					}],
					finishTime: [{
						required: true,
						message: "结束时间不能为空",
						trigger: "blur"
					}],
					safetyExaminationType: [{
						required: true,
						message: "考试状态不能为空",
						trigger: "change"
					}],
					setId: [{
						required: true,
						message: "设置人员不能为空",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
			this.getDicts("safety_examination_type").then(response => {
				for (let i = 0; i < 3; i++) {
					this.typeOptions[i] = response.data[i];
				}
			});
		},
		methods: {
			/** 查询考试信息列表 */
			getList() {
				this.loading = true;
				listSafetyexam().then(response => {
					this.examinationinfoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
				listTestpaper().then(response => {
					this.testpaperOptions = response.rows;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					safetyExaminationId: null,
					safetyExaminationName: null,
					startTime: null,
					finishTime: null,
					safetyExaminationType: null,
					setId: null,
					setDate: null,
					safetyExaminationPaperName: null
				};
				this.resetForm("form");
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.safetyExaminationId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 添加考试操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加考试信息";
				this.form.setId = store.getters.userId;
			},
			/** 查看详情按钮操作 */
			handleUpdate(row) {
				this.reset();
				const safetyExaminationId = row.safetyExaminationId || this.ids
				testPaperDetail(safetyExaminationId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改考试信息";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.safetyExaminationId != null) {
							modifySafetyexam(this.form).then(response => {
								this.msgSuccess("修改考试信息成功!");
								this.open = false;
								this.getList();
							});
						} else {
							addSafetyexam(this.form).then(response => {
								this.msgSuccess("添加考试成功!");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const safetyExaminationIds = row.safetyExaminationId || this.ids;
				this.$confirm('是否确认删除考试号为"' + safetyExaminationIds + '"的安全考试?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return deleteSafetyexam(safetyExaminationIds);
				}).then(() => {
					this.getList();
					this.msgSuccess("删除成功");
				});
			},
			/** 开始考试按钮操作 */
			Startexam(row) {
				const that = this;
				const safetyExaminationIds = row.safetyExaminationId || this.ids;
				this.$confirm('是否确认开始考试号为"' + safetyExaminationIds + '"的安全考试?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "success"
				}).then(function() {
					that.examParams.safetyExaminationId = safetyExaminationIds;
					that.examParams.safetyExaminationType = "1";
					that.examParams.setId = store.getters.userId;
					return modifySafetyexam(that.examParams);
				}).then(() => {
					this.getList();
					this.msgSuccess("开始考试！");
				})
			},
			/** 结束考试按钮操作 */
			Finishexam(row) {
				const that = this;
				const safetyExaminationIds = row.safetyExaminationId || this.ids;
				this.$confirm('是否确认结束考试号为"' + safetyExaminationIds + '"的安全考试?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					that.examParams.safetyExaminationId = safetyExaminationIds;
					that.examParams.safetyExaminationType = "0";
					that.examParams.setId = store.getters.userId;
					return modifySafetyexam(that.examParams);
				}).then(() => {
					this.getList();
					this.msgSuccess("结束考试！");
				})
			}
		}
	};
</script>
